package cn.ac.iie.di.ban.data.exchange.runner.slave;

import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.ScopeEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.StatusEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.TaskTypeEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.order.TaskOrderRequest;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.order.TaskOrderResponse;
import cn.ac.iie.di.ban.data.exchange.runner.commons.worker.SlaveWorker;
import cn.ac.iie.di.ban.data.exchange.runner.slave.task.checkTask.CheckSQLAndPermissionTask;
import cn.ac.iie.di.ban.data.exchange.runner.slave.task.runTask.BatchSubscriptionTask;
import org.apache.commons.lang.exception.ExceptionUtils;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by Black-Laptop on 12/6/2017.
 */
public class BatchSlave implements SlaveWorker {

    public static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchSlave.class);

    private TaskTypeEnum type;
    private ScopeEnum scope;

    private BatchSubscriptionTask batchSubscriptionTask;

    private TaskOrderRequest taskOrderRequest;
    private String uniq_key;
    private String nameServer;

    public static FinishedHook callAfterFinished;

    private String ftpIp;
    private int ftpPort;
    private String ftpUser;
    private String ftpPassword;

    /**
     * TaskTypeEnum.CHECK 功能时不需要穿任何参数
     */
    public BatchSlave() {
    }

    /**
     * ScopeEnum.MSG_REAL功能和ScopeEnum.MSG_BATCH功能需要用到nameServer参数
     *
     * @param nameServer
     */
    public BatchSlave(String nameServer) {
        this.nameServer = nameServer;

    }
//    public BatchSlave(String nameServer,String ftpIp,int ftpPort,String ftpUser, String ftpPassword) {
//        LOG.info("SubscriptionSlave  "+"nameServer:"+nameServer);
//        this.nameServer = nameServer;
//        this.ftpIp = ftpIp;
//        this.ftpPort = ftpPort;
//        this.ftpUser = ftpUser;
//        this.ftpPassword = ftpPassword;
//    }


    @Override
    public String getName() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public TaskOrderResponse execute(TaskOrderRequest taskOrderRequest, FinishedHook callAfterFinished) {

        this.type = taskOrderRequest.getType();
        this.scope = taskOrderRequest.getScope();
        BatchSlave.callAfterFinished = callAfterFinished;

        this.taskOrderRequest = taskOrderRequest;
        this.uniq_key = taskOrderRequest.getUniq_key();

        LOG.info("excute  " + "type:" + type + "  scope:" + scope + "  uniq_key" + uniq_key);

        TaskOrderResponse taskOrderResponse = null;

        try {
            taskOrderResponse = start();
            return taskOrderResponse;
        } catch (Exception e) {
            LOG.error(ExceptionUtils.getFullStackTrace(e));
            taskOrderResponse = new TaskOrderResponse(uniq_key, StatusEnum.BATCH_SUBSCRIPTION_TASK_ERROR, "start task error.");
        }

        return taskOrderResponse;

    }

    @Override
    public boolean finished() {
        return getBatchSubscriptionTaskThreadStatus();
    }

    @Override
    public void stop() {
        stopBatchSubscriptionTaskThread();
    }

    private TaskOrderResponse start() throws Exception {

        TaskOrderResponse taskOrderResponse = null;

        switch (this.type) {
            case CHECK: {
                CheckSQLAndPermissionTask checkSQLAndPermissionTask = new CheckSQLAndPermissionTask(this.taskOrderRequest);
                taskOrderResponse = checkSQLAndPermissionTask.checkSQLAndPermission(uniq_key);
                break;
            }
            case RUN: {
                taskOrderResponse = runTask();
                break;
            }
            default:
                break;
        }

        return taskOrderResponse;
    }

    private Boolean getBatchSubscriptionTaskThreadStatus() {
        if (this.batchSubscriptionTask != null) {
            return batchSubscriptionTask.getBatchSubscriptionTaskThreadStatus();
        }
        return false;
    }


    private void stopBatchSubscriptionTaskThread() {
        if (this.batchSubscriptionTask != null) {
            batchSubscriptionTask.stopBatchSubscriptionTaskThread();
        }
    }


    private TaskOrderResponse runTask() throws Exception {
        CheckSQLAndPermissionTask checkSQLAndPermissionTask = new CheckSQLAndPermissionTask(this.taskOrderRequest);
        checkSQLAndPermissionTask.checkSQLAndPermission(uniq_key);

        TaskOrderResponse taskOrderResponse = null;

        Boolean doBatchSubscription = ScopeEnum.MSG_BATCH.equals(scope);
        if (doBatchSubscription) {
            batchSubscriptionTask = new BatchSubscriptionTask(this.taskOrderRequest, nameServer);
            taskOrderResponse = batchSubscriptionTask.doBatchSubscriptionTask();
            return taskOrderResponse;
        }

        return taskOrderResponse;
    }


}
